import express from "express";
import router from "./router";
import cors from "cors";
const app = express();
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(cors());

app.use((req, res: any, next) => {
  res.cc = (err: any, status = 500) => {
    res.send({
      status,
      msg: err instanceof Error ? err.message : err,
    });
  };
  next();
});

router(app);

app.get("/", (req, res) => {
  res.send("Hello World!");
});
app.use((err: any, req: any, res: any, next: () => void) => {
  // 如果错误是由token解析失败导致的
  console.log(err);
  res.send({
    status: 500,
    msg: err,
  });
  next();
});
app.listen(80, () => {
  console.log("http://127.0.0.1");
});
